<?php
/**
 * Library class for PHP-SVN-Changelog
 * @author Ladislav Prskavec <ladislav@prskavec.net>
 * @copyright Copyright (c) 2007-2008 
 * @package Changelog 
 * @version $Id: changelog_class.php 12 2008-04-27 17:51:10Z abtris $
 */
/**
 * Class Changelog
 * @static
 * @package Changelog
 */
class Changelog
{
    /**
     * Constructor
     */
    private function __construct()
    {
        // empty 
    }
    /**
     * Date replace
     *
     * Change format from ISO to DD.MM.YYYY
     * 
     * @param string $date
     * @return string 
     */ 
    public static function date_replace($date)
    {
        $date=date_parse($date);
        $date=sprintf("%02d.%02d.%04d %02d:%02d:%02d",$date['day'],$date['month'],$date['year'],$date['hour'],$date['minute'],$date['second']);
        return $date;
    }
    /**
     * Check date range
     * @param string $date1 in DATE_ISO8601     
     * @param int days
     * return bool
     */
    public static function date_range($date1,$range)
    {
        $date1=date_parse($date1);
        $date2=date(DATE_ISO8601); // actual date
        $date2=date_parse($date2);
        if ($date1['year']==$date2['year'])
        {
            if ($date1['month']==$date2['month'])
            {
                if ($date1['day']==$date2['day'] || $date1['day']+$range==$date2['day'])
                {
                    return true;
                }
            }
        }
        
        return false;
    }
    /**
     * Truncate 
     * @param string $text Text ke zkrácení
     * @param int $numb Kolik znaků je platných
     * @param string $etc='...' Způsob zakončení
     * @return string
     */
    public static function truncate($text,$numb,$etc = "...")
    {
        if (strlen($text) > $numb)
        {
        $text = substr($text, 0, $numb);
        $text.= $etc;
        }
        return $text;
    }
    
    
    /**
     * Changelog
     * 
     * Struktura changelogu 
     * <code>
     * YYYY-MM-DD John Doe johndoe@example.com
     *
     *  * myfile.ext (myfunction): my changes made
     *   additional changes
     *
     *   * anotherfile.ext (somefunction): more changes
     * </code>
     * Struktura XML vystupu z SVN
     * <code>
     * <log>
     * <logentry revision="165">
     * <author>abtris</author>
     * <date>2008-03-26T13:36:35.586000Z</date>
     * <msg>
     * Ticket: 000041
     * Napojeni nazvu ve filtru na db, table status
     * </msg>
     * </logentry>
     * </log>
     * </code>
     * @param string $path
     * @param bool $last
     */ 
    public static function changelog($path,$last)
    {   
        // kontrola existence XML logu
        if (file_exists($path))
        {
          // kontrola validity XML
          $xml = @simplexml_load_file($path);
          if (!$xml)
          {
           if (!$last) echo "<h2 class='error'>Changelog in $path isn't valid XML file.</h2>";
           return false;
          }
     
        }
        else
        {
         // pokud neexituje soubor
         echo "<h2>Failed to open ".$path."</h2";
         exit;
        }
        
        // last revision
        if ($last)
        {            
            echo "<td class='msg'>";
            echo "<strong>";                        
            printf("R%d %s %s",$xml->logentry['revision'],self::date_replace($xml->logentry->date),$xml->logentry->author);
            echo "</strong>";
            echo "<br />";
            echo nl2br(self::message_replace($xml->logentry->msg));
            echo "</td>";
        }
        else
        {                        
            // all revisions
            echo "<table id='changelog'>";
            echo "<tr><th>Revision</th><th>Message</th><th>Date</th><th>Author</th></tr>";
             
            foreach ($xml->children() as $i)
            {               
               if (self::date_range($i->date,0))
               {
                echo "<tr class='thisday'>";
               }
               elseif (self::date_range($i->date,1))
               {
                echo "<tr class='dayback'>";
               }
               else
               {
                echo "<tr>\n";
               }
               // revision
               echo "<td>".$i['revision']."</td>";
               // msg
               echo "<td class='msg'>".nl2br(htmlspecialchars($i->msg));
               // path
               if (!empty($i->paths->path))
               {
                    echo "<div class='path'>";
                    foreach ($i->paths->path as $k)
                    {
                        echo $k['action']." - ".self::truncate($k,78)."<br />";                     
                    }
                    echo "</div>";
               }
               echo "</td>";
               // date
               echo "<td>";               
               echo self::date_replace($i->date);
               echo "</td>";
               // author
               echo "<td>".$i->author."</td>";
               echo "</tr>"; 
              
            }
            echo "</table>";
        }

    } // end changelog
     
} // end class
?>